<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap{
	/**
	 *
	 * @var unknown_type
	 */
	public $frontcontroller;
	/**
	 *
	 * @var unknown_type
	 */
	protected $_logger;
	/**
	 *
	 * @var unknown_type
	 */
	protected $_acl;
	/**
	 *
	 * @var unknown_type
	 */
	protected $_auth;

	/**
	 * Setup the logging
	 */
	protected function _initLogging()
	{
		$this->bootstrap('frontController');
		$logger = new Zend_Log();

		$writer = 'production' == $this->getEnvironment() ?
		new Zend_Log_Writer_Stream(APPLICATION_PATH . '/../data/logs/app.log') :
		new Zend_Log_Writer_Firebug();
		$logger->addWriter($writer);

		if ('production' == $this->getEnvironment()) {
			$filter = new Zend_Log_Filter_Priority(Zend_Log::CRIT);
			$logger->addFilter($filter);
		}

		$this->_logger = $logger;
		Zend_Registry::set('log', $logger);
	}
	protected function _initDefaultModuleAutoloader(){
		$this->_logger->info('Bootstrap '. __METHOD__);

		$this->_resourceLoader = new Zend_Application_Module_Autoloader(array(
			'namespace' =>	'EventManager',
			'basePath'	=>	APPLICATION_PATH . '/modules/eventManager',
		));

		$this->_resourceLoader->addResourceTypes(array(
				'modelResource'	=>	array(
				'path'			=>	'models/resources',
				'namespace'		=>	'Resource',
		),
				'service'	=> array(
				'path'		=>	'services',
				'namespace'	=>	'Service'
				),
				));
	}
	
		// @todo develop this function
	protected function _initDbProfiler(){

		$this->_logger->info('Bootstrap ' . __METHOD__);

		//if ('production' !== $this->getEnvironment()) {
			$this->bootstrap('db');
			$profiler = new Zend_Db_Profiler_Firebug('All DB Queries');
			$profiler->getQueryProfiles(null, true);
			$profiler->setEnabled(true);
			$this->getPluginResource('db')->getDbAdapter()->setProfiler($profiler);
		//}
	}

	/**
	 * Add Controller Action Helpers
	 */
	protected function _initActionHelpers()
	{
		$this->_logger->info('Bootstrap ' . __METHOD__);
		Zend_Controller_Action_HelperBroker::addHelper(new SF_Controller_Helper_Acl());
		Zend_Controller_Action_HelperBroker::addHelper(new SF_Controller_Helper_RedirectCommon());
		Zend_Controller_Action_HelperBroker::addHelper(new SF_Controller_Helper_Service());
	}
	/**
	 *
	 * @return unknown_type
	 */
	protected function _initRoutes(){
		
		$this->_logger->info('Initialize Routes '. __METHOD__);
		$this->bootstrap('frontController');
		$router = $this->frontController->getRouter();

		$route = new Zend_Controller_Router_route(
			'register',
		array(
				'controller'	=> 'user',
				'action'		=>	'register'
				)
				);
				$router->addRoute('register',$route);

				$route = new Zend_Controller_Router_route(
			'login',
				array(
				'controller'	=> 'user',
				'action'		=>	'login'
				)
				);
				$router->addRoute('login',$route);
				$route = new Zend_Controller_Router_route(
			'logout',
				array(
				'controller'	=> 'user',
				'action'		=>	'logout'
				)
				);
				$router->addRoute('logout',$route);

	}
	/**
	 *
	 * @return void
	 */
	protected function _initLocale(){

		$this->_logger->info('Bootstrap '.__METHOD__);
		$locale = new Zend_Locale('nl_NL');
		Zend_Registry::set('Zend_Locale', $locale);
	}
	
	protected function _initAcl(){
		$this->_acl = new EventManager_Service_Acl();
	}
	/**
	 *
	 * @return void
	 */
	protected function _initViewSettings(){
		$this->_logger->info('Bootstrap '.__METHOD__);
		$this->bootstrap('view');
		$this->bootstrap('Acl');
		$this->_view = $this->getResource('view');

		//set encoding and doctype
                $this->_view->addHelperPath("ZendX/JQuery/View/Helper/",
                                            "ZendX_JQuery_View_Helper");
		$this->_view->setEncoding('UTF-8');
		$this->_view->doctype('XHTML1_STRICT');
		$this->_view->headMeta()->appendHttpEquiv('Content-Type', 'text/html; charset=UTF-8');
		$this->_view->headMeta()->appendHttpEquiv('Content-Language', 'en-US');
			
		//@todo op een later moment moeten hier nog de stylesheets worden toegevoegd
		$this->_view->headLink()->appendStylesheet('/css/main.css');
		//$this->_view->headTitle('Event Manager');
		/*
		Set the head style.
		$this->_view->headTitle->headStyle();
		*/
		$this->_view->headTitle()->setSeparator(' - ');
		
		$this->_auth = Zend_Auth::getInstance();	

		$navigation = new Zend_Config_Xml(APPLICATION_PATH.'/configs/navigation.xml','nav');
		$navContainer = new Zend_Navigation($navigation);
		$this->_view->navigation($navContainer);
		//->setAcl($this->_acl)->setRole($this->_auth->getStorage()->read()->usr_role);

	}
	
	protected function _initDefaultEmailTransport()
	{
		$emailConfig = $this->getOption('email');
		$smtpHost = $emailConfig["transportOptionsSmtp"]["host"];
		unset($smtpHost);
		$mailTransport = new Zend_Mail_Transport_Smtp('smtp.gmail.com',$emailConfig["transportOptionsSmtp"]);  
		Zend_Mail::setDefaultTransport($mailTransport);
	}
	
	public function _initDatabaseProfiler(){
		$profiler = new Zend_Db_Profiler_Firebug('All DB Queries');
		$profiler->setEnabled(true);
		$this->getPluginResource('db')->getDbAdapter()->setProfiler($profiler);
	}
	
	public function initActionHelper(){
		$writer = new Zend_Log_Writer_Firbug();
		$logger = new Zend_Log($writer);
		Zend_Registry::set('logger', $logger);
	}
	
	/**
	 * Add graceful error handling to the dispatch, this will handle
	 * errors during Front Controller dispatch.
	 
	public function run()
	{
		
		$errorHandling = $this->getOption('errorhandling');
		var_dump($errorHandling);
		exit;
		try {
			parent::run();
		} catch(Exception $e) {
			if (true == (bool) $errorHandling['graceful']) {
				throw $e;
				$this->__handleErrors($e, $errorHandling['email']);
			} else {
				throw $e;
			}
		}
		
	}
	
	

	/**
	 * Handle errors gracefully, this will work as long as the views,
	 * and the Zend classes are available
	 *
	 * @param Exception $e
	 * @param string $email
	 */
	protected function __handleErrors(Exception $e, $email)
	{
		exit;
		header('HTTP/1.1 500 Internal Server Error');
		$view = new Zend_View();
		$view->addScriptPath(dirname(__FILE__) . '/../views/scripts');
		echo $view->render('fatalError.phtml');

		if ('' != $email) {
			$mail = new Zend_Mail();
			$mail->setSubject('Fatal error in application Storefront');
			$mail->addTo($email);
			$mail->setBodyText(
			$e->getFile() . "\n" .
			$e->getMessage() . "\n" .
			$e->getTraceAsString() . "\n"
			);
			@$mail->send();
		}
	}
}

